package com.dragon.www.haier.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dragon.www.haier.R;
import com.dragon.www.haier.entity.DeviceInfo;
import com.dragon.www.haier.net.NetHelper;
import com.dragon.www.haier.net.NetUtils;

import org.dragonnova.lamp.Lamp;

import java.util.ArrayList;
import java.util.List;

/**
 * HarerLampAdapter
 * Created by haojinyu on 10月27 027.
 */
public class HaierLampAdapter extends BaseAdapter {

    private Context mContext;
    private List<Lamp> mLamps;
    private List<Integer> controldata = new ArrayList<>();
//    private LampController mLampController;

    public HaierLampAdapter(Context context, List<Lamp> list) {
        mContext = context;
        mLamps = list;
//        mLampController = controller;
    }

    @Override
    public int getCount() {
        return mLamps.size();
    }

    @Override
    public Object getItem(int position) {
        return mLamps.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Lamp lamp = (Lamp) getItem(position);
        final LampViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new HaierLampAdapter.LampViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.haier_lamp_view_item, parent, false);
            viewHolder.splitbutton = (ImageView) convertView.findViewById(R.id.splitbutton);
            viewHolder.splitbutton1 = (ImageView) convertView.findViewById(R.id.splitbutton1);
            viewHolder.tv_lamp_desc = (TextView) convertView.findViewById(R.id.tv_lamp_desc);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (LampViewHolder) convertView.getTag();
        }
//        viewHolder.tv_lamp_desc.setText(String.format("%s-%s", HexUtil.bytesToString(new byte[]{(byte) lamp.getGroup()}), String.valueOf(lamp.getId())));
        viewHolder.tv_lamp_desc.setText(lamp.getmasterId()+"-"+lamp.getslaveId()+"-"+position);
        if (lamp.getStatus() == Lamp.ON) {
            viewHolder.splitbutton.setActivated(true);
            viewHolder.splitbutton1.setActivated(true);
            controldata.add(position);

        } else if (lamp.getStatus() == Lamp.OFF) {
            viewHolder.splitbutton.setActivated(false);
            viewHolder.splitbutton1.setActivated(false);
        }

        viewHolder.splitbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("haier", "tv_lamp_button");
//                mLampController.control(lamp, lamp.getStatus() == Lamp.ON ? Lamp.OFF : Lamp.ON);

                if (lamp.getStatus() == 0){
                    controldata.add(position);
                    viewHolder.splitbutton.setActivated(true);
                    viewHolder.splitbutton1.setActivated(true);
                    lamp.setStatus(1);
                }else {
                    controldata.remove((Integer)position);
                    viewHolder.splitbutton.setActivated(false);
                    viewHolder.splitbutton1.setActivated(false);
                    lamp.setStatus(0);
                }
                ControlHaierDevices(controldata,lamp.getmasterId(),lamp.getslaveId(),lamp.getdevicetype(),lamp.getcommtype());
//                lamp.setStatus(lamp.getStatus() == Lamp.ON ? Lamp.OFF : Lamp.ON);
//                ControlHaierDevices((Integer[])controldata.toArray());
            }
        });
        return convertView;

    }

    public class LampViewHolder extends AppHolder {
        public ImageView splitbutton;
        public ImageView splitbutton1;
        public TextView tv_lamp_desc;
    }

    public void ControlHaierDevices(List<Integer> controldata,int masterId,int slaveId,int devicetype,int commtype) {
        NetHelper.ControlHaierDevices(mContext, masterId, slaveId, devicetype, commtype, controldata,new NetUtils.NetCallBack<DeviceInfo>() {
            @Override
            public void success(DeviceInfo rspData) {

            }

            @Override
            public void failed(String msg) {
                Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
            }
        }, DeviceInfo.class);
    }
}
